package cpu.footprint.ui;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.Map;

import cpu.footprint.R;
import cpu.footprint.api.ApiUrl;
import cpu.footprint.api.MyApplication;
import cpu.footprint.bean.BaseBean;
import cpu.footprint.bean.GroupModel;
import cpu.footprint.bean.User;
import cpu.footprint.common.BaseActivity;
import cpu.footprint.api.APIService;
import cpu.footprint.api.RespCallBack;
import cpu.footprint.util.CreateDialogUtil;
import cpu.footprint.util.FileUtil;

/**
 * Created by Administrator on 2017/3/26.
 */
@ContentView(R.layout.activity_groupinfo)
public class GroupInfoActivity extends BaseActivity {
    @ViewInject(R.id.title_back)
    public ImageView title_back;//点击弹出左侧滑
    @ViewInject(R.id.image_groupImg)
    public ImageView image_groupImg;//点击弹出左侧滑
    @ViewInject(R.id.text_groupName)
    private TextView text_groupName;
    @ViewInject(R.id.text_groupNumber)
    private TextView text_groupNumber;
    @ViewInject(R.id.text_groupCreateTime)
    private TextView text_groupCreateTime;
    @ViewInject(R.id.text_groupDesc)
    private TextView text_groupDesc;
    private User user = new User();
    private GroupModel mGroupModel = new GroupModel();
    private int groupId;
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();
        myApplication= (MyApplication) this.getApplication();
        mContext=GroupInfoActivity.this;
        if(myApplication.isLogin()){
            user=myApplication.getUser();
        }else{
            Toast.makeText(GroupInfoActivity.this, "请先登录", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(GroupInfoActivity.this, LoginActivity.class);
            intent.putExtra("IsLogin", false);
            startActivity(intent);
            GroupInfoActivity.this.overridePendingTransition(0,R.anim.fade);
            GroupInfoActivity.this.finish();
            return;
        }
        groupId=this.getIntent().getIntExtra("sqlId",0);
        getGroupInfo();
    }

    private void getGroupInfo() {
        Map<String, String> map = new HashMap<String, String>();
        map.put("mobilePhone", user.getMobilePhone());
        map.put("token", user.getToken());
        map.put("sqlId", groupId+"");
        mDialog.show();
        APIService.Post(ApiUrl.URL_GET_GROUP, map, new RespCallBack<BaseBean<GroupModel>>(this) {
            @Override
            public void onSuccess(BaseBean<GroupModel> result) {
                super.onSuccess(result);
                if (result.success) {
                    mGroupModel = result.data;
                    initData();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                mDialog.dismiss();
            }
        });
    }

    /**
     * 初始化View
     */
    private void initView() {
        mDialog=CreateDialogUtil.CreateDialog(this,"请稍后...",null);
    }

    /**
     * 填充数据
     */
    private void initData() {
        if(mGroupModel!=null){
            text_groupName.setText(mGroupModel.getName());
            text_groupNumber.setText(mGroupModel.getId());
            String time ="";
            try {
                time=sdf.format(mGroupModel.getCreateTime());
            }catch (Exception e){
                e.printStackTrace();
                time=e.getMessage();
            }
            text_groupCreateTime.setText("本群创建于"+time);
            text_groupDesc.setText("\u3000\u3000"+mGroupModel.getDescription());
            String fileName=FileUtil.getInstance().getImageName(mGroupModel.getHeadpicture());
            String headPath=ApiUrl.URL_FILEPATH+fileName;
            if(FileUtil.fileIsExists(headPath)){
                image_groupImg.setImageBitmap(BitmapFactory.decodeFile(headPath));
            }else if(mGroupModel.getHeadpicture()!=null&&!mGroupModel.getHeadpicture().equals("")) {
                String imgUrl = ApiUrl.URL_HOST+mGroupModel.getHeadpicture();
                Picasso.with(this).load(imgUrl).placeholder(R.mipmap.head).into(image_groupImg);
            }
        }
    }
    /**
     * 点击事件监听
     * @param v
     */
    @Event(value = {R.id.layout_groupManage,R.id.image_title_back,R.id.layout_groupNotice,R.id.layout_groupCard,R.id.layout_groupMember,R.id.image_title_menu})
    private void OnClick(View v){
        switch (v.getId()){
            case R.id.image_title_back:{
                setResult(1);
                this.finish();
                overridePendingTransition(0,R.anim.fade);
            }break;
            case R.id.layout_groupManage:{
                Intent intent=new Intent(GroupInfoActivity.this,GroupManageActivity.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("group",mGroupModel);
                intent.putExtra("bundle",bundle);
                startActivityForResult(intent,1);
            }break;
            case R.id.layout_groupNotice:{
                Intent intent=new Intent(GroupInfoActivity.this,GroupNoticeActivity.class);
                intent.putExtra("groupId",mGroupModel.getSqlId());
                startActivityForResult(intent,2);
            }break;
            case R.id.layout_groupCard:{

            }break;

            case R.id.layout_groupMember:{
                Intent intent=new Intent(GroupInfoActivity.this,GroupMemberActivity.class);
                intent.putExtra("groupId",mGroupModel.getSqlId());
                startActivityForResult(intent,4);
            }break;
            case R.id.image_title_menu:{

            }break;
            default:
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        switch (requestCode){
            case 1:{
                if(resultCode==1){
                    //有更新
                }else{
                    //没有更新
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
    /**
     * 重写后退函数
     * @param keyCode
     * @param event
     * @return
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
            GroupInfoActivity.this.finish();
            overridePendingTransition(0,R.anim.fade);
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }


}
